/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package uvexposureapplication.utils;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;
import java.util.logging.Level;
import java.util.logging.Logger;
import uvexposureapplication.sensors.data.SensorsDataFormat;
import uvexposureapplication.server.contextmanager.CityData;
import uvexposureapplication.server.preferencerepository.UserData;

/**
 *
 * @author matthieukluj
 */
public class IOStream {

    public static BufferedReader readFileBuffer(String fileName) {
        BufferedReader br = null;
        try {
            System.out.println("Reading File " + fileName);
            FileInputStream fstream = new FileInputStream(fileName);
            // Get the object of DataInputStream
            DataInputStream in = new DataInputStream(fstream);
            br = new BufferedReader(new InputStreamReader(in));
        } catch (Exception e) {
            System.err.append(e.getMessage());
        }
        return br;
    }

    public static String readFile(String fileName) {
        String fileText = "";
        try {

            BufferedReader br = readFileBuffer(fileName);
            String strLine;
            while ((strLine = br.readLine()) != null) {
                fileText += strLine;
            }

        } catch (IOException ex) {
            Logger.getLogger(IOStream.class.getName()).log(Level.SEVERE, null, ex);
        }
        return fileText;
    }

    public static List<SensorsDataFormat> readSensorsData(String fileName) {
        List<SensorsDataFormat> inputLines = new LinkedList<SensorsDataFormat>();
        String strLine;
        BufferedReader br = readFileBuffer(fileName);
        try {
            while ((strLine = br.readLine()) != null) {
                SensorsDataFormat data = new SensorsDataFormat();
                StringTokenizer st = new StringTokenizer(strLine, ",");
                while (st.hasMoreTokens()) {
                    data.setValue(st.nextToken());
                    data.setTime(Integer.parseInt(st.nextToken()));
                }
                inputLines.add(data);
            }
        } catch (IOException ex) {
            Logger.getLogger(IOStream.class.getName()).log(Level.SEVERE, null, ex);
        }
        return inputLines;
    }

    public static Map<String, String> readLocationsData(String fileName) {
        Map<String, String> locationData = new HashMap<String, String>();
        String strLine;
        BufferedReader br = readFileBuffer(fileName);
        try {
            while ((strLine = br.readLine()) != null) {
                StringTokenizer st = new StringTokenizer(strLine, ":");
                while (st.hasMoreTokens()) {
                    String type = st.nextToken();
                    type = type.substring(0, type.length() - 1);
                    StringTokenizer st2 = new StringTokenizer(st.nextToken(), ",");
                    while (st2.hasMoreTokens()) {
                        String locationLine = st2.nextToken();
                        if (locationLine.length() > 1) {
                            Character location = locationLine.charAt(1);
                            locationData.put(location.toString(), type);
                        }
                    }
                }
            }
        } catch (IOException ex) {
            Logger.getLogger(IOStream.class.getName()).log(Level.SEVERE, null, ex);
        }
        return locationData;
    }

    public static List<UserData> readUsersData(String fileName) {
        List<UserData> userDatas = new LinkedList<UserData>();
        String strLine;
        BufferedReader br = readFileBuffer(fileName);
        try {
            UserData userData = new UserData();
            while ((strLine = br.readLine()) != null) {
                if (strLine.isEmpty()) {
                    userDatas.add(userData);
                } else {
                    StringTokenizer st = new StringTokenizer(strLine, ":");
                    while (st.hasMoreTokens()) {
                        String param = st.nextToken();
                        String value = st.nextToken().substring(1);
                        if (param.equals("name")) {
                            userData = new UserData();
                            userData.setName(value);
                        } else if (param.equals("Skin Type")) {
                            userData.setSkinType(Integer.parseInt(value));
                        } else if (param.equals("pref-1") || param.equals("pref-2")) {
                            String[] valueTable = value.split(" ");
                            if (valueTable[1].equals("UV")) {
                                userData.addSuggestion("UV", valueTable[3]);
                            } else {
                                userData.addSuggestion(valueTable[1], valueTable[3]);
                                userData.setTempThreshold(Integer.parseInt(valueTable[1]));
                            }
                        }
                    }
                }
            }
            userDatas.add(userData);
        } catch (IOException ex) {
            Logger.getLogger(IOStream.class.getName()).log(Level.SEVERE, null, ex);
        }
        return userDatas;
    }

    public static List<CityData> readCityData(String fileName) {
        List<CityData> cityDatas = new LinkedList<CityData>();
        String strLine;
        BufferedReader br = readFileBuffer(fileName);
        try {
            CityData cityData = new CityData();
            while ((strLine = br.readLine()) != null) {
                if (!strLine.isEmpty()) {
                    StringTokenizer st = new StringTokenizer(strLine, ":");
                    while (st.hasMoreTokens()) {
                        String param = st.nextToken();
                        String value = st.nextToken().substring(1);
                        if (param.equals("name")) {
                            cityData = new CityData();
                            cityData.setName(value);
                        } else if (param.equals("location")) {
                            cityData.setLocation(value);
                        } else if (param.equals("information")) {
                            cityData.setInformation(value);
                        } else if (param.equals("services")) {
                            StringTokenizer st2 = new StringTokenizer(value, ",");
                            while (st2.hasMoreTokens()) {
                                cityData.addService(st2.nextToken());
                            }
                            cityDatas.add(cityData);
                        }
                    }
                }
            }
        } catch (IOException ex) {
            Logger.getLogger(IOStream.class.getName()).log(Level.SEVERE, null, ex);
        }
        return cityDatas;
    }

    public static String readLine() {
        String line = "";
        try {
            InputStreamReader converter = new InputStreamReader(System.in);
            BufferedReader in = new BufferedReader(converter);
            line = in.readLine();
        } catch (IOException ex) {
            Logger.getLogger(IOStream.class.getName()).log(Level.SEVERE, null, ex);
        }
        return line;
    }
}
